package com.bz.xtcx.manager.mapper.provider;

import com.bz.xtcx.manager.persist.conditions.LibCondition;
import com.bz.xtcx.manager.utils.QuerySplitUtil;
import com.bz.xtcx.manager.utils.WzStrUtil;
import com.bz.xtcx.manager.vo.VoQuery;
import org.springframework.util.StringUtils;

import java.util.List;

public class LibServiceProvider {
	
	public String findByConditionApi(LibCondition qc) {
		StringBuilder sql = new StringBuilder("select l.id, l.number,l.address, l.name,l.org_type,i.field1 as logo, l.services_provided ");
		if(qc.isLoadAll()) {
			sql.append(",l.org_nature ");
		}
		sql.append(" from `lib_service` l left join `lib_image` i on l.id=i.lib_id ");
		sql.append(" where l.status = 1 ");
		if(!StringUtils.isEmpty(qc.getObjName())) {

			List<String> paramList = QuerySplitUtil.getParamList(qc.getObjName());
			sql.append(" and (");
			for (int i = 0; i < paramList.size(); i++) {
				String param = paramList.get(i);
				if(i != 0){
					sql.append(" or ");
				}
				sql.append(" l.name like '%").append(param).append("%' ");
				sql.append(" or l.org_type like '%").append(param).append("%' ");
				sql.append(" or l.search_param like '%").append(param).append("%' ");
			}
			sql.append(")");
		}
        if (WzStrUtil.isNotBlank(qc.getObjCountry())) {
            sql.append(" and l.country like '%").append(qc.getObjCountry()).append("%' ");
        }
        if (WzStrUtil.isNotBlank(qc.getObjDomain())) {
            sql.append(" and l.org_type like '%").append(qc.getObjDomain()).append("%' ");
        }
        if (WzStrUtil.isNotBlank(qc.getProvince())) {
            sql.append(" and l.search_param like '%").append(qc.getProvince()).append("%' ");
        }
        if (WzStrUtil.isNotBlank(qc.getCity())) {
            sql.append(" and l.search_param like '%").append(qc.getCity()).append("%' ");
        }
        if (WzStrUtil.isNotBlank(qc.getZone())) {
            sql.append(" and l.search_param like '%").append(qc.getZone()).append("%' ");
        }
		// add by wangtao on 201901301600 for 门户资源列表倒序 start
		sql.append(" order by l.update_time desc, l.name ");
		// add by wangtao on 201901301600 for 门户资源列表倒序 start
        return sql.toString();
	}

	public String findByCondition(VoQuery e) {
		StringBuilder sql = new StringBuilder("select * from `lib_service` where 1=1 ");
		sql.append(queryCondition(e));
        return sql.toString();
	}
	
	StringBuilder queryCondition(VoQuery e) {
		StringBuilder sql = new StringBuilder();
		if(e != null) {
			if(e.getUserId() != null) {
				sql.append(" and user_id = '").append(e.getUserId()).append("'");
			}
			if (!StringUtils.isEmpty(e.getStatus())) {
				sql.append(" and status = '"+e.getStatus()+"'");
			}
			/*if (!StringUtils.isEmpty(e.getCheckStatus())) {
				sql.append(" and check_status = '"+e.getCheckStatus()+"'");
			}*/
			if (!StringUtils.isEmpty(e.getCreditLevel())) {
				sql.append(" and credit_level = '"+e.getCreditLevel()+"'");
			}
			if (!StringUtils.isEmpty(e.getObjName())) {

				List<String> paramList = QuerySplitUtil.getParamList(e.getObjName());
				sql.append(" and (");
				for (int i = 0; i < paramList.size(); i++) {
					String param = paramList.get(i);
					if(i != 0){
						sql.append(" or ");
					}
					sql.append(" name like '%"+param+"%' ");
					sql.append(" or org_code like '%"+param+"%' ");
					sql.append(" or number like '%"+param+"%' ");
					sql.append(" or org_type like '%"+param+"%' ");
					sql.append(" or search_param like '%").append(param).append("%' ");
				}
				sql.append(")");
			}
            if (WzStrUtil.isNotBlank(e.getObjCountry())) {
                sql.append(" and country like '%").append(e.getObjCountry()).append("%' ");
            }
            if (WzStrUtil.isNotBlank(e.getObjDomain())) {
                sql.append(" and org_type like '%").append(e.getObjDomain()).append("%' ");
            }
            if (WzStrUtil.isNotBlank(e.getProvince())) {
                sql.append(" and search_param like '%").append(e.getProvince()).append("%' ");
            }
            if (WzStrUtil.isNotBlank(e.getCity())) {
                sql.append(" and search_param like '%").append(e.getCity()).append("%' ");
            }
            if (WzStrUtil.isNotBlank(e.getZone())) {
                sql.append(" and search_param like '%").append(e.getZone()).append("%' ");
            }
		}
		return sql;
	}
}
